package com.ezjie.ielts.module_word.adapter;

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.ezjie.ielts.R;
import com.ezjie.ielts.db.WordDao;
import com.ezjie.ielts.db.WordInstanceDao;
import com.ezjie.ielts.db.WordMeaningDao;
import com.ezjie.ielts.model.Eword;
import com.ezjie.ielts.model.EwordInstance;
import com.ezjie.ielts.model.EwordMeaning;
import com.ezjie.ielts.model.Mean;
import com.ezjie.ielts.model.WordBean;
import com.ezjie.ielts.util.ListUtils;

/**
 * vip课程adapter
 * 
 * @author renyangyang
 * 
 */
public class FirstStudySummaryAdapter extends BaseExpandableListAdapter {
	private Context context;
	private List<WordBean> mList;
	private WordDao wordDao;
	private WordInstanceDao wordInstanceDao;
	private WordMeaningDao wordMeaningDao;
	private LayoutInflater mInflater;

	public FirstStudySummaryAdapter(Context context) {
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		wordDao = new WordDao(context);
		wordInstanceDao = new WordInstanceDao(context);
		wordMeaningDao = new WordMeaningDao(context);
	}

	public void setmList(List<WordBean> mList) {
		this.mList = mList;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mList.get(groupPosition).getMeans();
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
		ViewHolder holder;
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.summary_child_item, null);
			holder = new ViewHolder();
			holder.mean = (TextView) view.findViewById(R.id.tv_means);
			holder.soundmark = (TextView) view.findViewById(R.id.soundmark);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		WordBean mBean = mList.get(groupPosition);
		if (mBean != null) {
			List<Mean> means = mBean.getMeans();
			holder.mean.setText(Html.fromHtml(ListUtils.getTransferStrByList(means)));
			holder.soundmark.setText(Html.fromHtml(mBean.getPhonetic()));
		}
		return view;
	}

	class ViewHolder {
		TextView word, soundmark, mean;
		View item_header_line, view_fooder_line;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return mList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mList == null ? 0 : mList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.summary_group_item, null);
			holder = new ViewHolder();
			holder.mean = (TextView) view.findViewById(R.id.mean);
			holder.soundmark = (TextView) view.findViewById(R.id.soundmark);
			holder.word = (TextView) view.findViewById(R.id.word);
			holder.item_header_line = view.findViewById(R.id.item_header_line);
			holder.view_fooder_line = view.findViewById(R.id.view_fooder_line);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		if (groupPosition == 0) {
			holder.item_header_line.setVisibility(View.GONE);
		} else {
			holder.item_header_line.setVisibility(View.VISIBLE);
		}
		if (isExpanded) {
			holder.view_fooder_line.setVisibility(View.GONE);
		} else {
			holder.view_fooder_line.setVisibility(View.VISIBLE);
		}

		WordBean mBean = mList.get(groupPosition);
		if (mBean != null) {
			if (TextUtils.isEmpty(mBean.getWord()) || TextUtils.isEmpty(mBean.getPhonetic())) {
				// 从数据库读取
				Eword eword = wordDao.findByWordId(mBean.getWid());
				List<EwordInstance> ewordInstances = wordInstanceDao.findByWordId(mBean.getWid());
				List<EwordMeaning> ewordMeanings = wordMeaningDao.findByWordId(mBean.getWid());
				if (eword != null) {
					mBean.setWord(eword.word);
					if (TextUtils.isEmpty(eword.en_phonetic)) {
						mBean.setPhonetic("暂无");
					} else {
						mBean.setPhonetic(eword.en_phonetic);
					}
					mBean.setInstances(ListUtils.transferInstanceList(ewordInstances));
					mBean.setMeans(ListUtils.transferMeanList(ewordMeanings));

				}
			}
			List<Mean> means = mBean.getMeans();
			// 显示第一个释义
			holder.mean.setText(Html.fromHtml(ListUtils.getTransferStr1ByList(means)));
			holder.soundmark.setText("");

			holder.word.setText(Html.fromHtml(mBean.getWord()));
			if (isExpanded) {
				holder.mean.setVisibility(View.GONE);
			} else {
				holder.mean.setVisibility(View.VISIBLE);
			}
		}
		return view;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}

}
